package cannot.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cannot.mapper.MainTypeMapper;
import cannot.mapper.OtherTypeMapper;
import cannot.pojo.MainType;
import cannot.pojo.OtherType;
import cannot.pojo.OtherTypeExample;
import cannot.service.MainTypeService;

@Service
public class MainTypeServiceImpl implements MainTypeService {

	@Autowired
	MainTypeMapper mainTypeMapper;
	@Autowired
	OtherTypeMapper otherTypeMapper;
	
	public List<MainType> listMainType() {
		return mainTypeMapper.selectByExample(null);
	}

	public void updateMainType(MainType mainType) {
		mainTypeMapper.updateByPrimaryKey(mainType);
	}

	public int insertMainType(MainType mainType) {
		return mainTypeMapper.insert(mainType);
	}

	//删除主分类时删除其下的附属分区
	public void deleteMainType(Integer id) {
		OtherTypeExample otherTypeExample = new OtherTypeExample();
		otherTypeExample.createCriteria().andForeginkeyEqualTo(id);
		List<OtherType> OtherTypes =  otherTypeMapper.selectByExample(otherTypeExample);
		for(OtherType otherType:OtherTypes){
			otherTypeMapper.deleteByPrimaryKey(otherType.getId());
		}
		mainTypeMapper.deleteByPrimaryKey(id);
	}

	public MainType selectById(Integer id) {
		return mainTypeMapper.selectByPrimaryKey(id);
	}

}
